package com.fzmdev.test_new_jacoco;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class MainActivity extends AppCompatActivity {

	public static String TAG = "[CodeCoverage]";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Log.d(TAG, "after onCreate()");
		Log.d(TAG, "run 1 + 2 = " + MathCalc.add(1, 2));

		Log.d(TAG, "run 1 * 2 = " + MathCalc.multiply(1, 2));
		Log.d(TAG, "run 1 / 2 = " + MathCalc.divide(1, 2));
		Log.d(TAG, "hello run 1 / 2 = " + MathCalc.divide(3, 1));

		// 判断用户是否授权, 如果未授权时, 则需要让用户授权
		if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
			// 弹出授权窗口
			ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
		} else {
			Log.d(TAG, "用户已授权");
		}

		Button btn = findViewById(R.id.btn1);
		Button btn2 = findViewById(R.id.btn2);
		Button btn3 = findViewById(R.id.btn3);
		Button btn44 = findViewById(R.id.btn4);
		Button btn5 = findViewById(R.id.btn5);
		Button btn6 = findViewById(R.id.btn6);
		Button btn7 = findViewById(R.id.btn7);
		Button btn8 = findViewById(R.id.btn8);

		btn.setOnClickListener(view -> {
			Log.d(TAG, "点击了btn");
		});

		btn2.setOnClickListener(view -> {
			Log.d(TAG, "点击了btn2");
		});

		btn3.setOnClickListener(view -> {
			Log.d(TAG, "点击了btn3");
		});

		btn44.setOnClickListener(view -> {
			Log.d(TAG, "点击了btn44");
			Log.d(TAG, "点击了btn44: 02");
		});

		btn5.setOnClickListener(view -> {
			Log.d(TAG, "点击了btn5");
		});

		btn6.setOnClickListener(view -> {
			Log.d(TAG, "点击了btn6");
		});

		btn7.setOnClickListener(view -> {
			Log.d(TAG, "点击了btn777");
		});

		btn8.setOnClickListener(view -> {
			Log.d(TAG, "点击了btn888");
		});
	}

	private void generateCoverageReport() {
		String DEFAULT_COVERAGE_FILE_PATH = Environment.getExternalStorageDirectory().getPath() + "/data.exec";
		File file = new File(DEFAULT_COVERAGE_FILE_PATH);
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				Log.d(TAG, "异常 : " + e);
				e.printStackTrace();
			}
		}
		Log.d(TAG, "generateCoverageReport():" + DEFAULT_COVERAGE_FILE_PATH);
		try {
			OutputStream out = new FileOutputStream(DEFAULT_COVERAGE_FILE_PATH, true);
			Object agent = Class.forName("org.jacoco.agent.rt.RT")
					.getMethod("getAgent")
					.invoke(null);

			out.write((byte[]) agent.getClass().getMethod("getExecutionData", boolean.class)
					.invoke(agent, false));
			out.close();
		} catch (Exception e) {
			Log.d(TAG, e.toString(), e);
		}
	}

	@Override
	protected void onDestroy() {
		Log.d(TAG, "before onDestroy()");
		super.onDestroy();
		Log.d(TAG, "after onDestroy()");
		generateCoverageReport();
	}
}